package com.shop.service.pay;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.common.StringUtil;
import com.shop.entity.BaseOrderDetailDo;
import com.shop.entity.BaseOrderDo;
import com.shop.entity.SellerCommodityExtendDo;
import com.shop.entity.SystemParameterDo;
import com.shop.mapper.BaseOrderDetailDoMapper;
import com.shop.mapper.BaseOrderDoMapper;
import com.shop.mapper.SellerCommodityExtendDoMapper;
import com.shop.mapper.ext.SellerCommodityExtendDoMapperExt;
import com.shop.service.SystemParameterService;
import com.shop.shopEnum.SystemParameterEnum;
import com.shop.shopEnum.YesOrNoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class AbService {


    @Autowired
    private SystemParameterService systemParameterService;

    @Autowired
    private BaseOrderDetailDoMapper baseOrderDetailDoMapper;

    @Autowired
    private SellerCommodityExtendDoMapperExt sellerCommodityExtendDoMapperExt;

    @Autowired
    private BaseOrderDoMapper baseOrderDoMapper;

    public Map<String, List<SystemParameterDo>> payParam(Integer type) throws Exception {
        List<SystemParameterDo> systemParameterDos = systemParameterService.selectSystemParameterByType(type);
        return systemParameterDos.stream().collect(Collectors.groupingBy(SystemParameterDo::getSystemKey));
    }


    public BaseOrderDo getBaseOrderDo(Long pkBaseOrderId) {
        BaseOrderDo baseOrderDo = baseOrderDoMapper.selectById(pkBaseOrderId);
        Assert.notNull(baseOrderDo, "查询不到订单！");
        return baseOrderDo;
    }

    public String commodityNo(Long pkBaseOrderId) throws Exception {
        QueryWrapper<BaseOrderDetailDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_base_order_id", pkBaseOrderId);
        queryWrapper.eq("is_payment", YesOrNoEnum.NO.code);
        queryWrapper.eq("is_del", YesOrNoEnum.NO.code);
        List<BaseOrderDetailDo> baseOrderDetailDos = baseOrderDetailDoMapper.selectList(queryWrapper);
        if (baseOrderDetailDos.size() <= 0) throw new Exception("没有要付款的商品！");
        List<SellerCommodityExtendDo> sellerCommodityExtendDoList = sellerCommodityExtendDoMapperExt.selectList(baseOrderDetailDos.stream().map(BaseOrderDetailDo::getFkSellerCommodityExtendId).collect(Collectors.toList()));
        List<String> commodityName = sellerCommodityExtendDoList.stream().map(SellerCommodityExtendDo::getCommodityName).collect(Collectors.toList());
        return StringUtil.join(commodityName, "_");
    }

}
